<?php
/**
 * @desc 商品管理
 * @author kevin wang
 * @date 2014-10-25
 */
class ctrl_produce extends ctrl_base {
    private static $dirName = 'produce';
    
    /**
     * @desc 商品列表
	 * @author kevin wang
	 * @param void
	 * @return void
     */
    public static function main() {
    	if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		self::operation(array('produce_small_class'));
		//页面输入参数处理
    	$currentPage = isset($_POST['pageNum']) ? $_POST['pageNum'] : 1;
    	$pagesize = isset($_POST['numPerPage']) ? $_POST['numPerPage'] : self::$_pagesize;
		$nextpage = ($currentPage - 1) * $pagesize;

    	$parent_id = isset($_POST['parent_id']) ? intval($_POST['parent_id']) : 0;
		$class_small_name = isset($_POST['class_small_name']) ? trim($_POST['class_small_name']) : '';
    	//搜索条件组合
		$condition = array();
    	if(!empty($parent_id)) {
    		$condition['parent_id'] = $parent_id;
    	}
		if(!empty($class_small_name)) {
    		$condition['class_small_name'] = $class_small_name;
    	}
		//订单获取
    	$produce_list = model_produce :: get_small_class_list($nextpage,$pagesize,$condition);
    	$total = array_pop($produce_list);
		$produce_class = model_produce::get_type_list();
		//模板赋值
    	self::smarty() -> assign('produce_list', $produce_list);
		self::smarty() -> assign('total', $total);
		self::smarty() -> assign('pageNum', $currentPage);
		self::smarty() -> assign('numPerPage', $pagesize);
		self::smarty() -> assign('pageNumShown', self::$_pageNumShown);
		self::smarty() -> assign('parent_id', $parent_id);
		self::smarty() -> assign('class_small_name', $class_small_name);
		self::smarty() -> assign('shoNum', self::$_shoNum);
		self::smarty()->assign('produce_class', $produce_class);
		
    	self::smarty() -> display(self::$dirName . '/small_class.html');    	
    }
	//添加商品
	public static function small_class_add(){
		if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		self::operation(array('small_class_add'));
		if($_POST){

			$produce = self::get_produce_data();

			$produce['author'] = $_SESSION['admin']['username'];

			$result = model_produce::small_class_add($produce);
			if(!empty($result)){
				self::ajax_ouput(200, '操作成功','small_class','closeCurrent');
			}else{
				self::ajax_ouput(300, '添加失败');
			}
		}
		$type = model_produce::get_type_list();
		self::smarty() -> assign("type", $type);
		//运费模板
		$motor = model_transporttmp::get_transporttmp_by_type(1);
		self::smarty() -> assign("motor", $motor);
		$train = model_transporttmp::get_transporttmp_by_type(2);
		self::smarty() -> assign("train", $train);
		$ship = model_transporttmp::get_transporttmp_by_type(3);
		self::smarty() -> assign("ship", $ship);
		$smotor = model_transporttmp::get_transporttmp_by_type(4);
		self::smarty() -> assign("smotor", $smotor);

		self::smarty() -> display(self::$dirName . '/small_class_add.html');
	}
	//删除商品
	public static function small_class_delete() {
    	if(!model_admin::islogin()) {
			self::goto_login();
    	}
		self::operation(array('small_class_delete'));
    	$ids = $_POST['ids'];

    	$st = model_produce::delete_product($ids);

    	if($st > 0) {
			self::ajax_ouput(200, '操作成功','small_class');
    	} else {
			self::ajax_ouput(300, '操作失败');
    	}
    }
	//编辑商品
	 public static function small_class_edit() {
    	if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		$id = isset($_GET['id'])? (intval($_GET['id'])):(intval($_POST['id']));
    	if($id <= 0) {
			self::ajax_ouput(300, '非法访问');
    	}
		$produce = model_produce::get_produce_by_id($id);
		$ctrl_tag = 'small_class_edit_other';
		if($produce['author'] == $_SESSION['admin']['username']){
			$ctrl_tag = 'small_class_edit_self';
		}
		self::operation(array($ctrl_tag));
    	if($_POST){
			$produce = array();

			$produce = self::get_produce_data();

			$result = model_produce::update_produce_info($id, $produce);
	    	
		    if(!empty($result)){
				self::ajax_ouput(200, '操作成功','small_class','closeCurrent');
			}else{
				self::ajax_ouput(300, '操作失败');
			}    		
    	}
		$type = model_produce::get_type_list();
		self::smarty() -> assign("type", $type);
		//运费模板
		$motor = model_transporttmp::get_transporttmp_by_type(1);
		self::smarty() -> assign("motor", $motor);
		$train = model_transporttmp::get_transporttmp_by_type(2);
		self::smarty() -> assign("train", $train);
		$ship = model_transporttmp::get_transporttmp_by_type(3);
		self::smarty() -> assign("ship", $ship);
		$smotor = model_transporttmp::get_transporttmp_by_type(4);
		self::smarty() -> assign("smotor", $smotor);

    	self::smarty() -> assign('id', $id);
		$produce['basic_price'] = model_produce::get_basic_price($id);
    	self::smarty() -> assign('produce', $produce);
    	self::smarty() -> display(self::$dirName . '/small_class_edit.html');
    }
	//区域选择
	/*public static function area(){
		if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		$id = intval($_GET['id']);
		$name = trim($_GET['name']);
		$area = model_produce::get_area_list();
		$area = self::area_by_group($area);
		$user_select = model_produce::get_transport_province($id, $name);
		$user_select = explode(",", $user_select);
		self::smarty() -> assign("select",$user_select);
		self::smarty() -> assign('area', $area);
		self::smarty() -> display(self::$dirName . '/area.html');
	}*/
	//获取数据
	private static function get_produce_data(){
		$produce = self::get_request_data();
		$list['produce_name'] = $produce['produce_name'];
		$list['parent_id'] = $produce['parent_id'];
		$list['area_motor_id'] = $produce['area_motor_id'];
		$list['area_smotor_id'] = $produce['area_smotor_id'];
		$list['area_ship_id'] = $produce['area_ship_id'];
		$list['area_train_id'] = $produce['area_train_id'];
		$list['status'] = $produce['status'];
		$basic_price = array();
		foreach($produce['marerial'] as $k => $v){
			if(empty($v)){
				continue;
			}
			$basic_price[$k]['marerial'] = $v;
			$basic_price[$k]['markup'] = $produce['markup'][$k];
			$basic_price[$k]['show_sort'] = $produce['show_sort'][$k];
		}
		if(empty($basic_price)){
			self::ajax_ouput(300, '操作失败,请添加产品材质');
		}
		$list['basic_price'] = $basic_price;
		$list['editer'] = $_SESSION['admin']['username'];
		return $list;
	}
	//区域选择
	public static function area(){
		if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		$id = intval($_GET['id']);
		$name = trim($_GET['name']);
		$area = model_produce::get_area_list();
		$list = self::area_by_group($area);
		$user_select = array();
		if(!empty($id) && !empty($name)){
			$user_select = model_produce::get_transport_province($id, $name);
			$user_select = explode(",", $user_select);
		}
		self::smarty() -> assign("select",$user_select);
		self::smarty() -> assign('list', $list);
		self::smarty() -> display(self::$dirName . '/area.html');
	}
	//地区分组
	/*private static function area_by_group(array $area){
		$return = self::$words;
		foreach($area as $key => $value){
			$first = strtoupper($value['sortkey'][0]);
			$return[$first][] = $value;
		}
		return $return;
	}*/
	private static function area_by_group(array $area){
		$return = array();
		$provice = array();
		$list = array();
		foreach($area as $key => $value){
			$provice[] = $value['province'];
			if(!empty($value['street'])){
				$list[$value['province']][$value['city']][$value['area'].'/'.$value['street']]['id'] = $value['area_id'];
				$list[$value['province']][$value['city']][$value['area'].'/'.$value['street']]['name'] = "{$value['province']}/{$value['city']}/{$value['area']}/{$value['street']}";
			}else{
				$list[$value['province']][$value['city']][$value['area']]['id'] = $value['area_id'];
				$list[$value['province']][$value['city']][$value['area']]['name'] = "{$value['province']}/{$value['city']}/{$value['area']}/{$value['street']}";
			}
		}
		$provice = array_unique($provice);
		$return['provice'] = $provice;
		$return['list'] = $list;
		return $return;
	}
	//拼接数组的值
	private static function get_value_of_array($arr){
		$return = json_encode($arr);
		if(empty($arr) || is_string($arr)){
			return $return;
		}
		$return = "";
		foreach($arr as $v){
			if(!empty($v)){
				$return[] = $v;
			}
		}
		$return = json_encode($return);
		return $return;
	}
}
